package com.example.demo1013.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo1013.entity.Student;
import com.example.demo1013.mapper.StudentMapper;
import com.example.demo1013.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    private static final Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);

    @Override
    public Student getStudentById(String studentId) {
        logger.info("查询学生信息，studentId：{}", studentId);
        Student student = baseMapper.selectById(studentId);
        logger.info("查询结果，student：{}", student);
        return student;
    }

    @Override
    public List<Student> getAllStudents() {
        List<Student> students = baseMapper.selectList(null);
        logger.info("查询所有学生信息：学生数量={}", students.size());
        return students;
    }

    @Override
    public List<Student> getStudentsByCondition(Integer status) {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getStatus, status);
        List<Student> students = baseMapper.selectList(wrapper);
        logger.info("按状态查询结果：学生数量={}", students.size());
        return students;
    }

    @Override
    public void updateStudent(Student student) {
        logger.info("更新学生信息：student={}", student);
        baseMapper.updateById(student);
    }

    @Override
    public void addStudent(Student student) {
        logger.info("新增学生信息：student={}", student);
        baseMapper.insert(student);
    }

    @Override
    public List<Student> getStudentsByName(String name) {
        logger.info("通过名字搜索学生信息：name={}", name);
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Student::getName, name)
                .eq(Student::getStatus, 1);
        List<Student> students = baseMapper.selectList(wrapper);
        logger.info("查询结果：学生数量={}", students.size());
        return students;
    }
}